package westh.ilib.struts.action.surveyReport;

import java.text.SimpleDateFormat;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import westh.ilib.bean.surveyReport.SurveyReport;
import westh.ilib.service.SurveyReportService;

/**
 * @author ZhangYanxing
 */
public class GetSurveyReportAction extends Action {
	private final static Log log = LogFactory
			.getLog(GetSurveyReportAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// get 'surveyExpression'
		String surveyExpression = request.getParameter("surveyExpression");
		System.out.println("surveyExpression = " + surveyExpression);

		// get 'rscTypeList'
		String[] resType = request.getParameterValues("resourceType");
		log.debug("resTypeList = " + Arrays.toString(resType));

		// get 'rscFieldList'
		String[] fieldType = request.getParameterValues("resourceField");
		log.debug("resFieldList = " + Arrays.toString(fieldType));

		/* write surveyLog into ULAScloud */
		String resTypeStr = Arrays.toString(resType);
		String resTypeRequestStr = "";
		for (int i = 1; i <= 7; i++) {
			if (resTypeStr.indexOf(Integer.toString(i)) != -1) {
				resTypeRequestStr += "1";
			} else {
				resTypeRequestStr += "0";
			}
		}
		log.debug(resTypeRequestStr);
		
		String resFieldStr = Arrays.toString(fieldType);
		String resFieldRequestStr = "";
		for (int i = 1; i <= 5; i++) {
			if (resFieldStr.indexOf(Integer.toString(i)) != -1) {
				resFieldRequestStr += "1";
			} else {
				resFieldRequestStr += "0";
			}
		}
		log.debug(resFieldRequestStr);
		
		try {
			JSONObject logJson = new JSONObject();
			logJson.put("methodName", "saveSurveyLog");

			JSONObject jsonData = new JSONObject();

			jsonData.put("userId", "null");

			SimpleDateFormat tempDate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String queryTimeString = tempDate.format(new java.util.Date());
			jsonData.put("time", queryTimeString);

			jsonData.put("query", surveyExpression);

			jsonData.put("rscTypeList", resTypeRequestStr);
			
			jsonData.put("fieldList", resFieldRequestStr);

			logJson.put("jsonStr", jsonData);

			String logJsonStr = logJson.toString();

			westh.log.YyLog.rulesSave(request, logJsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}

		/* generate SurveyReport result */
		//SurveyReport report = SurveyReportService.getSurveyReport(surveyExpression);
		SurveyReport report = SurveyReportService.getSurveyReport(
				surveyExpression, resType, fieldType);
		request.setAttribute("report", report);

		// unit test
		log.debug("book: " + report.getBookNum());
		log.debug("conference: " + report.getConfNum());
		log.debug("Journal: " + report.getJournalNum());
		log.debug("patent: " + report.getPatentNum());
		log.debug("softwCopyr: " + report.getSoftwCopyrNum());
		log.debug("standard: " + report.getStandardNum());
		log.debug("techReport: " + report.getTechReportNum());
		 
		/* write survey result into ULAScloud */
		try {
			JSONObject resultJson = new JSONObject();
			resultJson.put("methodName", "saveSurveyReport");

			JSONObject resultData = new JSONObject();

			SimpleDateFormat tempDate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String queryTimeString = tempDate.format(new java.util.Date());
			resultData.put("time", queryTimeString);

			resultData.put("query", surveyExpression);

			resultData.put("rscTypeList", resTypeRequestStr);

			resultData.put("fieldList", resFieldRequestStr);
			
			resultData.put("survBookNum", report.getBookNum());
			resultData.put("survJournalNum", report.getJournalNum());
			resultData.put("survConfNum", report.getConfNum());
			resultData.put("survPatentNum", report.getPatentNum());
			resultData.put("survSoftwCopyrNum", report.getSoftwCopyrNum());
			resultData.put("survStandarNum", report.getStandardNum());
			resultData.put("survTechReportNum", report.getTechReportNum());
			resultData.put("survAbstract", report.getSurveyAbstract());
			
			resultJson.put("jsonStr", resultData);

			String resultJsonStr = resultJson.toString();

			westh.log.YyLog.rulesSave(request, resultJsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mapping.findForward("succ");
		
		/*
		String[] rscType = new String[5];
		rscType[0] = "1";
		rscType[1] = "2";
		rscType[2] = "3";
		rscType[3] = "4";
		rscType[4] = "5";

		String[] rscField = new String[1];		
		rscField[0] = "5";
		
		SurveyReportService.getSurveyHistory("search engine", rscType, rscField);
		*/
	}
}